/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.renderer;

import com.jb.web.bean.inspector.DefaultPropertyEditor;
import com.jb.web.bean.inspector.PropertyField;
import com.jb.web.context.WebContext;

import com.jb.faces.component.UIComponent;


public class PropertyEditorRenderer extends DefaultRenderer {
    public StringBuffer render(WebContext pCtx, UIComponent itemToRender) {
        PropertyField formItem = (PropertyField) itemToRender;
        StringBuffer html = new StringBuffer("");

        if (formItem.getLabelVisible()) {
            html.append(renderLabel(pCtx, formItem));
        }

        html.append("<TD ").append(addTagProperty(" class", "propertyFieldCell", null)).append(" >");

        DefaultPropertyEditor pe = (DefaultPropertyEditor) formItem.getPropertyEditor(pCtx);
        html.append(pe.render(pCtx, itemToRender));
        html.append("</TD>");

        return html;
    }

    protected String getLabelText(WebContext pCtx, PropertyField formItem) {
        return (String) evaluate(pCtx, formItem, formItem.getLabel());
    }

    /**
     *
     *
     * @param pCtx parameter
     * @param formItem parameter
     *
     * @return return-value
     */
    protected String renderLabel(WebContext pCtx, PropertyField formItem) {
        StringBuffer html = new StringBuffer("<TD ").append(addTagProperty("nowrap", "nowrap", null))
                                                    .append(addTagProperty("align", "right", "right"))
                                                    .append(addTagProperty("class", "propertyFieldLabel", null))
                                                    .append(" >").append(getLabelText(pCtx, formItem)).append(formItem.getMandatory()
                ? "*" : "");

        return html.append("</TD>").toString();
    }
}
